package cn.lbd.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ZKExists {
    private CountDownLatch countDownLatch = null;
    private ZooKeeper zooKeeper = null;
    private String socketAddress = "39.97.191.1:2181";

    @Before
    public void init() throws Exception {
        countDownLatch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(socketAddress, 5000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("连接建立成功");
                    //连接成功，通知计数器-1
                    countDownLatch.countDown();
                    System.out.println("通知状态 => "+event.getState());
                    System.out.println("事件类型 => "+event.getType());
                }
            }
        });
        countDownLatch.await();
        System.out.println("会话ID => " + zooKeeper.getSessionId());
    }

    @After
    public void destroy() throws InterruptedException {
        zooKeeper.close();
    }

    //同步
    @Test
    public void test1() throws KeeperException, InterruptedException {
        String path = "/create/demo17";
        Stat stat = zooKeeper.exists(path, false);
        if (stat == null) {
            System.out.println(path + "节点不存在");
        } else {
            System.out.println("节点创建时的事务ID => " + stat.getCzxid());
        }

    }

    //异步
    @Test
    public void test2() throws InterruptedException {
        zooKeeper.exists("/create/demo17", false, new AsyncCallback.StatCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, Stat stat) {
                if (rc == 0) {
                    System.out.println("创建节点成功");
                } else {
                    System.out.println("创建节点失败");
                }
                System.out.println("节点路径" + path);
                System.out.println(ctx);
                if (stat == null) {
                    System.out.println(path + "节点不存在");
                } else {
                    System.out.println("节点创建时的事务ID => " + stat.getCzxid());
                }
            }
        }, "这是一个上下文参数");
        TimeUnit.SECONDS.sleep(5);
        System.out.println("结束");
    }

    //使用连接对象的监视器
    @Test
    public void test3() throws InterruptedException, KeeperException {
        zooKeeper.exists("/create/demo20", true);
        TimeUnit.SECONDS.sleep(50);
        System.out.println("结束");
    }

    //自定义监视器
    @Test
    public void test4() throws InterruptedException, KeeperException {
        zooKeeper.exists("/create/demo20", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("通知状态 => "+event.getState());
                System.out.println("事件类型 => "+event.getType());
            }
        });
        TimeUnit.SECONDS.sleep(50);
        System.out.println("结束");
    }

    //注册多个监听器对象
    @Test
    public void test5() throws InterruptedException, KeeperException {
        zooKeeper.exists("/create/demo20", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("1");
                System.out.println("通知状态 => "+event.getState());
                System.out.println("事件类型 => "+event.getType());
            }
        });
        zooKeeper.exists("/create/demo20", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("2");
                System.out.println("通知状态 => "+event.getState());
                System.out.println("事件类型 => "+event.getType());
            }
        });
        TimeUnit.SECONDS.sleep(50);
        System.out.println("结束");
    }

    //将监视器注册动作放在process中，就可以多次使用监视器
    @Test
    public void test6() throws InterruptedException, KeeperException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("通知状态 => "+event.getState());
                System.out.println("事件类型 => "+event.getType());
                try {
                    zooKeeper.exists("/create/demo20", this);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.exists("/create/demo20", watcher);
        TimeUnit.SECONDS.sleep(50);
        System.out.println("结束");
    }


}
